﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SIAIllustrator.Calculator;
using SIAIllustrator.Index;

namespace SIAIllustrator
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {

            DataTable dt = null;

            try
            {
                TextReader tr = new StreamReader("index.csv");
                dt = IndexDAO.ParseCSV(tr.ReadToEnd());
            }
            catch (Exception exc)
            {
                MessageBox.Show("Unable to read source data.  Terminating application");
                // exit program code
            }

            Global.IndexTable = dt;


            Global.Illustration = new Illustration(dt);

            InitializeComponent();
        }

        private void btnSubmit_Click(object sender, RoutedEventArgs e)
        {


            double premium = double.Parse(txtPremium.Text);

            string type = Scenario.PeriodCertain;

            int deferralPeriod = int.Parse(txtDeferralPeriod.Text);
            int payoutPeriod = int.Parse(txtPayoutPeriod.Text);

            double deferralPartRate = double.Parse(txtDeferralPartRate.Text) / 100;
            double payoutPartRate = double.Parse(txtPayoutPartRate.Text) / 100;
            double guaranteedRate = double.Parse(txtGuaranteedRate.Text) / 100;

            int month = System.DateTime.Now.Month - 1;
            if (month == 0)
                month = 12;


            int year = System.DateTime.Now.Year;
            if (System.DateTime.Now.Month == 1)
                year--;


            int begYear = year - (deferralPeriod + payoutPeriod);
            int endYear = year;

            Scenario scenario = new Scenario(premium, type, month, begYear, endYear,
                                    deferralPeriod, payoutPeriod, deferralPartRate,
                                    payoutPartRate, guaranteedRate);

            Global.Illustration.AddScenario(scenario);

        }
    }
}
